﻿<?php 
require_once '../../includefiles.php';
include_once("../../model/users.php");
include_once("../../model/trade.php");
include_once("../../model/leads.php");
include_once("../../model/state.php");
include_once("../../model/budget.php");
require_once '../../model/message.php';
require_once '../../model/admindetails.php';
include_once '../../model/email.php';


$userObj = new Model_Users();
$tradeObj = new Model_TRADE();
$leadObj = new Model_Leads();
$objState = new Model_State();
$budgetObj = new Model_Budget();
$adminObj = new Model_AdminDetails();
$emailObj = new Model_Email();
$msgObj = new Model_Message();

## Check user login or not?
	$login_id = $_SESSION['getbids_id'];		
	
	if($login_id == ""){
	?> 
		<script type="text/javascript">		
		//SITEROOT = "http://192.168.1.42/getbids/";						
		SITEROOT = "<?php echo SITE_URL;?>/";
		
		var redirectPage = "signin/";	
		window.parent.location.href = redirectPage;		
		
        </script>
 	<?php
		exit;		
	} 
 
if(isset($_POST['submit']))
{
	extract($_POST);
	$userId = $_SESSION['getbids_id']; 

	$userDetArray = $userObj->getUserDetailsByUserId($userId);
	
	$smarty->assign('userDetArray',$userDetArray);
	
	$postjobid = base64_decode($_POST['jobid']);	
	// job detail	
	$jobdetArray = $userObj->getJobDetailsById($postjobid);
	// trade detail
	$gettradedetail = $tradeObj->getTradeCategoryDetailsById($jobdetArray['trade_id']);	
	
	//jobtype detail
	$getjobtypedetail = $tradeObj->getTradeCategoryDetailsById($jobdetArray['jobtype_id']);

	// homeowner detail
	$homeowneruserDetArray = $userObj->getUserDetailsByUserId($jobdetArray['user_id']);
	
	
	if($userDetArray['leads']!='0')
	{
		$getuserjobviewArr = $userObj->getUserViewjobDetailsByUserIdJobId($userId,$postjobid);
		if($getuserjobviewArr['id']=='')
		{
			// code for update leads
			$userArray = array();
			$userArray['leads']	= $userDetArray['leads']-1;
			$userObj->editUserValueById($userArray,$userId);	
			
			// code for add user job view table
			$userJobViewArray["userid"] 	= $userId;					
			$userJobViewArray["jobid"] 		= $postjobid;
			$userJobViewArray["message"] 		= stripslashes($_POST['message']);
			$userJobViewArray["date_added"] = date('Y-m-d');
			$jobviewId = $userObj->addUserViewJOBByValue($userJobViewArray);
			
			
				// send email message to home owner 
				$email_message = $emailObj->getEmailById(10);
				$e_subject = $email_message['email_subject'];
				$message = $email_message['email_content']; 
				
				$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		
			
				$companyname = str_replace(" ","_",trim($userDetArray["companyname"])); 
				$url = SITE_URL."/findtradesman/".$companyname;
				
				$message = str_replace('[HOME_NAME]', trim($homeowneruserDetArray["first_name"])." ".trim($homeowneruserDetArray["last_name"]) , $message);
				$message = str_replace('[SITENAME]', SITENAME, $message);
				$message = str_replace('[TRAD_NAME]', trim($userDetArray["first_name"])." ".trim($userDetArray["last_name"]) , $message);
				$message = str_replace('[EMAIL]', trim($userDetArray["email"]), $message);
				$message = str_replace('[CONTACTNO]', trim($userDetArray["phone_number"]), $message);
				$message = str_replace('[MESSAGE]', trim(stripslashes($_POST["message"])), $message);
				$message = str_replace('[URL]', $url, $message);
				$message = str_replace('[ABN]', $userDetArray["abn"], $message);
				$message = str_replace('[ACN]', $userDetArray["acn"], $message);
				
				
				
				$to= trim($homeowneruserDetArray["email"]);
				$adminEmail = $adminObj->getAdminDetails();
				$from = $adminEmail;
				$fromname= SITENAME;
								 
				$emailTemplate = file_get_contents('../../emailtemplate.html');
				$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
				$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
				$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
				//echo $template_msg; exit;
				if(trim($to) != '')
				@send_mail($to, $template_msg, $subject, $from, $fromname); 
				
				//message add for inbox homeowner
					$msgAddArray = array();
					$msgAddArray['from_id']   = '1'; 
					$msgAddArray['to_id'] 	  = $homeowneruserDetArray['id'];
					$msgAddArray['ad_id']     = $postjobid;
					$msgAddArray['subject']   = $subject;
					$msgAddArray['message']   = $message;
					$msgAddArray['send_date'] = getCurDate();
					$msgAddArray['label'] 	  = 'inbox';
					$msgObj->addMessage($msgAddArray); # add record for inbox
					
					$msgAddArray['label'] 	  = 'outbox';
					$msgObj->addMessage($msgAddArray); # add record for outbox
				
				
				// send email message to Admin
				$email_message = $emailObj->getEmailById(11);
				$e_subject = $email_message['email_subject'];
				$message = $email_message['email_content']; 
				
				$subject = str_replace('[NAME]', trim($userDetArray["first_name"])." ".trim($userDetArray["last_name"]) , $e_subject);		 
				
				$message = str_replace('[NAME]', trim($userDetArray["first_name"])." ".trim($userDetArray["last_name"]) , $message);
				$message = str_replace('[SITENAME]', SITENAME, $message);
				$message = str_replace('[TRADE]', trim($gettradedetail["trade_category"]), $message);
				$message = str_replace('[JOBTYPE]', trim($getjobtypedetail["trade_category"]), $message); 
				$message = str_replace('[JOBDESC]', trim($jobdetArray["job_desc"]), $message); 
				$message = str_replace('[HOME_NAME]', trim($homeowneruserDetArray["first_name"])." ".trim($homeowneruserDetArray["last_name"]) , $message);
				$message = str_replace('[TRADE_NAME]', trim($userDetArray["first_name"])." ".trim($userDetArray["last_name"]) , $message);
				
				
				$adminEmail = $adminObj->getAdminDetails();
				$to = $adminEmail;
				$from =$userDetArray["email"];
				$fromname= SITENAME;
								 
				$emailTemplate = file_get_contents('../../emailtemplate.html');
				$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
				$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
				$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
				//echo $template_msg; exit;
				if(trim($to) != '')
				@send_mail($to, $template_msg, $subject, $from, $fromname); 
				
				//message add for inbox admin
					$msgAddArray = array();
					$msgAddArray['from_id']   = $userDetArray["id"]; 
					$msgAddArray['to_id'] 	  = '1';
					$msgAddArray['ad_id']     = $postjobid;
					$msgAddArray['subject']   = $subject;
					$msgAddArray['message']   = $message;
					$msgAddArray['send_date'] = getCurDate();
					$msgAddArray['label'] 	  = 'inbox';
					$msgObj->addMessage($msgAddArray); # add record for inbox
					
					$msgAddArray['label'] 	  = 'outbox';
					$msgObj->addMessage($msgAddArray); # add record for outbox
				
				
				// send email message to Tradesman user 
				$email_message = $emailObj->getEmailById(12);
				$e_subject = $email_message['email_subject'];
				$message = $email_message['email_content']; 
				
				$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		 
						
				
				$message = str_replace('[TRADE]', trim($gettradedetail["trade_category"]), $message);
				$message = str_replace('[JOBTYPE]', trim($getjobtypedetail["trade_category"]), $message); 
				$message = str_replace('[JOBDESC]', trim($jobdetArray["job_desc"]), $message); 
				$message = str_replace('[JOBPOSTCODE]', trim($jobdetArray["area"]), $message); 
				$message = str_replace('[HOME_NAME]', trim($homeowneruserDetArray["first_name"])." ".trim($homeowneruserDetArray["last_name"]) , $message);
				$message = str_replace('[HOME_EMAIL]', trim($homeowneruserDetArray["email"]), $message);
				$message = str_replace('[HOME_MOBNO]', trim($homeowneruserDetArray["phone_number"]), $message);
				$message = str_replace('[SITENAME]', SITENAME, $message);
				
				$to= trim($userDetArray["email"]);
				$adminEmail = $adminObj->getAdminDetails();
				$from = $adminEmail;
				$fromname= SITENAME;
								 
				$emailTemplate = file_get_contents('../../emailtemplate.html');
				$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
				$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
				$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
				//echo $template_msg; exit;
				if(trim($to) != '')
				@send_mail($to, $template_msg, $subject, $from, $fromname); 
				
				//message add for inbox Tradesman
					$msgAddArray = array();
					$msgAddArray['from_id']   = '1';
					$msgAddArray['to_id']     = $userDetArray["id"]; 					
					$msgAddArray['ad_id']     = $postjobid;
					$msgAddArray['subject']   = $subject;
					$msgAddArray['message']   = $message;
					$msgAddArray['send_date'] = getCurDate();
					$msgAddArray['label'] 	  = 'inbox';
					$msgObj->addMessage($msgAddArray); # add record for inbox
					
					$msgAddArray['label'] 	  = 'outbox';
					$msgObj->addMessage($msgAddArray); # add record for outbox
			
		}
	}
	
	//redirect_to_link(SITE_URL."/leads/".$_POST['jobid']."/buylead/");	
	//$_SESSION['msg'] = "<div class='success_msg'><span>You have successfully shown interest for job, wait for homeowner reply!</span></div>";
	$_SESSION['msg'] = "<div class='success_msg'><span>We have just emailed the details of the lead to you.You can also view the contact details in <a href='" . SITE_URL . "/leads/myleads/'>My Leads</a></span></div>";
	$_SESSION['msg'] = "<div class='success_msg'><span>We have just emailed the details of the lead to you.</span></div>";
	
	//redirect_to_link(SITE_URL."/leads/");
	
	?> 
		<script type="text/javascript">		
		SITEROOT = "<?php echo SITE_URL ?>";
			
		var redirectPage = SITEROOT + "/leads/myleads";	
		window.parent.location.href = redirectPage;		
		
        </script>
 	<?php		
	exit;
} 
 
if($_SESSION['msg']){
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
	}

$smarty->display(TEMPLATEDIR . 'modules/leads/interest.tpl');
?>


